/**
 * 
 */
package neptune.web.client.controller;

import neptune.web.client.WebInterface;
import neptune.web.client.commands.ExperimentSummaryCommand;
import neptune.web.client.model.DefaultRoles;
import neptune.web.client.services.ApplicationDataProvider;
import neptune.web.client.services.user.LogInService;
import neptune.web.client.view.ConsoleManager;
import neptune.web.client.view.ExperimentSummaryView;
import neptune.web.client.view.ExperimentView;
import neptune.web.client.view.OperativeView;
import neptune.web.client.view.SuperView;
import neptune.web.client.view.UserView;
import neptune.web.client.view.ViewManager;
import neptune.web.client.view.components.AdminMenuBar;
import neptune.web.client.view.components.UserMenuBar;
import neptune.web.client.view.components.dialogs.PropertiesDialog;
import neptune.web.client.view.components.dialogs.PropertiesDialogListener;

import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * @author Roberto Bifulco [info@robertobifulco.it, robertobifulco.it]
 * 
 */
public class LogInDialogController implements PropertiesDialogListener {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * neptune.web.client.view.components.PropertiesDialogListener#okPressed
	 * (java.lang.String[][],
	 * neptune.web.client.view.components.PropertiesDialog)
	 */
	public void okPressed(final String[][] properties,
			final PropertiesDialog sender) {
		LogInService.Util.getInstance().login(properties[0][1],
				properties[1][1], new AsyncCallback<String>() {
					public void onFailure(Throwable arg0) {
						ConsoleManager.addConsoleMessage(arg0.getMessage(),
								true);

						sender.showMessage(arg0.getMessage());
						ApplicationDataProvider.getInstance().getTimer()
								.cancel();
					}

					public void onSuccess(String arg0) {
						ConsoleManager.addConsoleMessage(
								"Log in successed, your role is now " + arg0,
								false);

						OperativeView view = (OperativeView) ViewManager
								.getViewManager().getViewByName(
										OperativeView.DEFAULT_TOKEN);
						view.setUsernameLabel(properties[0][1], (String) arg0);

						ApplicationDataProvider.getInstance()
								.setActualUserRole((String) arg0);
						// Imposta l'intervallo di polling per il recupero dei
						// dati dal
						// server. Questa impostazione consente di mantenere
						// sempre
						// aperta la connessione client
						ApplicationDataProvider.getInstance().getTimer()
								.cancel();
						ApplicationDataProvider
								.getInstance()
								.getTimer()
								.scheduleRepeating(
										ApplicationDataProvider.AUTO_UPDATE_INTERVAL);

						if (((String) arg0).equals(DefaultRoles.SYSTEM_ADMIN)) {
							view.setTopMenuBar(new AdminMenuBar());
						} else {
							view.setTopMenuBar(new UserMenuBar());
						}

						WebInterface.updateViewsOfOperativeView();
						sender.hide();

						SuperView operative = (SuperView) ViewManager
								.getViewManager().getViewByName(
										OperativeView.DEFAULT_TOKEN);
						ExperimentSummaryView expView = (ExperimentSummaryView) operative
								.getViewByName(ExperimentSummaryView.DEFAULT_TOKEN);
						expView.reset();
						expView.update();

						UserView usrView = (UserView) operative
								.getViewByName(UserView.DEFAULT_TOKEN);
						if (usrView != null) {
							usrView.reset();
							usrView.update();
						}

						if (!ApplicationDataProvider.getInstance()
								.getActualUserName().equals(properties[0][1])) {
							ExperimentView expDetailsView = (ExperimentView) operative
									.getViewByName(ExperimentView.DEFAULT_TOKEN);
							if (expDetailsView != null) {
								if (operative.getActiveView() instanceof ExperimentView)
									(new ExperimentSummaryCommand()).execute();
								expDetailsView.resetView();
							}
						}

						ApplicationDataProvider.getInstance()
								.setActualUserName(properties[0][1]);
					}
				});

	}
}
